package light.head.controller.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.oa.InStockDAO;
import light.head.dao.oa.InStockItemDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.InStock;
import light.head.model.InStockItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/***
 * 物品入库记录
 * @author yy
 *
 */
@Controller
@RequestMapping(value = Url.C_INSTOCK_RECORD)
public class InStockController extends SupporterController{
	private InStockDAO isDAO;

	private InStockItemDAO isiDAO;
	private OrganDAO organDAO;
	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse reponse) throws Exception {
		return new ModelAndView(Url.INSTOCK_RECORD_INIT);
	}
	
	@RequestMapping(Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse reponse) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.DELETED,Parm.GN, Parm.CN, Parm.GN, Parm.TIME1,Parm.TIME2);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (RESULT.SUCCESS == isDAO.findByMap(organDAO.getSonCodeList(oc), pb, map)) {
			List<Object> insCodes = new ArrayList<Object>();
			for (Object o : pb.getResultList()) {
				InStock is = (InStock) o;
				insCodes.add(is.getCode());
			}
			// 将对应的明细放到对应的入库信息里
			List<InStockItem> isiList = isiDAO.findByInStockCodes(insCodes);
			if (null != isiList && isiList.size() > 0) {
				for (Object o : pb.getResultList()) {
					InStock is = (InStock) o;
					is.setItemList(new ArrayList<InStockItem>());
					for (InStockItem item : isiList) {
						if (is.getCode().intValue() == item.getSc().intValue()) {
							is.getItemList().add(item);
						}
					}
				}
			}
		}
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.INSTOCK_RECORD_LIST);
	}

	
	

	@Autowired
	public void setIsDAO(InStockDAO isDAO) {
		this.isDAO = isDAO;
	}

	@Autowired
	public void setIsiDAO(InStockItemDAO isiDAO) {
		this.isiDAO = isiDAO;
	}

	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}

	
	
}
